package com.juku2024.juku

import android.os.Build
import android.os.Bundle
import android.text.Html
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.text.method.ScrollingMovementMethod
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import com.juku2024.juku.base.BaseActivity
import com.juku2024.juku.model.UpdateModel
import com.juku2024.juku.tools.AlertUtils
import com.juku2024.juku.tools.AnalysisConstants
import com.juku2024.juku.tools.AnalysisUtils
import com.juku2024.juku.tools.AppUtils
import com.juku2024.juku.tools.Constants
import com.juku2024.juku.tools.RxUtils
import io.reactivex.disposables.Disposable

class UpgradeActivity: BaseActivity() {

    private val updateActivityStatus = StringBuilder("updateActivity -> ")

    private lateinit var tvTitle:TextView
    private lateinit var tvContent:TextView
    private lateinit var tvUpdate:TextView
    private lateinit var tvUpdateCancel:TextView

    // 返回双击
    private var exitDisposable: Disposable? = null
    private var updateModel: UpdateModel? = null

    override fun getLayoutResId(): Int {
        return R.layout.activity_upgrade
    }

    override fun initView() {
        super.initView()
        tvTitle = findViewById(R.id.tv_update_title)
        tvContent = findViewById(R.id.tv_update_content)
        tvUpdate = findViewById(R.id.tv_update)
        tvUpdateCancel = findViewById(R.id.tv_update_cancel)
    }

    @Suppress("DEPRECATION")
    override fun init(savedInstanceState: Bundle?) {
        // 添加回调对象
        onBackPressedDispatcher.addCallback(this, onBackPress)

        updateModel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent?.getParcelableExtra(Constants.INTENT_KEY_UPDATE, UpdateModel::class.java)
        } else {
            intent?.getParcelableExtra(Constants.INTENT_KEY_UPDATE) as UpdateModel?
        }
        if (updateModel == null) {
            updateActivityStatus.append("updateModel is null。")
            AnalysisUtils.analysis(AnalysisConstants.EVENT_UPGRADE, AnalysisConstants.KEY_UPDATE_ACTIVITY_STATUS, updateActivityStatus.toString())
            finish()
            return
        }
        updateActivityStatus.append("进入页面 -> ")

        tvTitle.text = updateModel?.title?:"升级更新"
        tvUpdate.text = updateModel?.btnTitle?:"升级"
        tvContent.movementMethod = ScrollingMovementMethod()
        // 开始响应点击事件
        tvContent.movementMethod = LinkMovementMethod.getInstance()
        // 加载富文本内容
        if (!TextUtils.isEmpty(updateModel?.txt)) {
            val text = if (Build.VERSION.SDK_INT >= 24)
                Html.fromHtml(updateModel?.txt, Html.FROM_HTML_MODE_COMPACT)
            else
                Html.fromHtml(updateModel?.txt)
            tvContent.text = text
        }

        val forceStr: String
        if (updateModel?.foreceUpdate == true) {
            forceStr = "强升 -> "
            tvUpdateCancel.text = getString(R.string.exit)
        } else {
            forceStr = "普通升级 -> "
            tvUpdateCancel.text = getString(R.string.cancel)
        }

        updateActivityStatus.append(forceStr)

        tvUpdate.setOnClickListener {
            if (updateModel == null || TextUtils.isEmpty(updateModel?.url)) {
                updateActivityStatus.append("升级 -> updateModel is null or url is null。")
                AnalysisUtils.analysis(AnalysisConstants.EVENT_UPGRADE, AnalysisConstants.KEY_UPDATE_ACTIVITY_STATUS, updateActivityStatus.toString())
                finish()
                return@setOnClickListener
            }
            updateActivityStatus.append("升级 -> 跳转 -> ")
            AppUtils.toWeb(updateModel?.url!!)
            if (!updateModel!!.foreceUpdate) {
                updateActivityStatus.append("非强升，关闭页面。")
                AnalysisUtils.analysis(AnalysisConstants.EVENT_UPGRADE, AnalysisConstants.KEY_UPDATE_ACTIVITY_STATUS, updateActivityStatus.toString())
                finish()
            }
        }

        // 右侧按钮点击事件
        tvUpdateCancel.setOnClickListener {
            // 统计是否强制升级
            if (updateModel?.foreceUpdate == true) {
                updateActivityStatus.append("取消 -> 强升，杀进程。")
                AnalysisUtils.analysis(AnalysisConstants.EVENT_UPGRADE, AnalysisConstants.KEY_UPDATE_ACTIVITY_STATUS, updateActivityStatus.toString())
                com.blankj.utilcode.util.AppUtils.exitApp()
            } else {
                updateActivityStatus.append("取消 -> 非强升，关闭页面。")
                AnalysisUtils.analysis(AnalysisConstants.EVENT_UPGRADE, AnalysisConstants.KEY_UPDATE_ACTIVITY_STATUS, updateActivityStatus.toString())
                finish()
            }
        }
    }


    // 返回处理
    private val onBackPress = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            if (updateModel == null) finish()
            if (updateModel?.foreceUpdate == true) {
                if (exitDisposable == null || exitDisposable?.isDisposed == true) {
                    AlertUtils.alert("再按一次退出程序")
                    exitDisposable = RxUtils.timer(2500) {}
                } else {
                    exitDisposable = null
                    updateActivityStatus.append("强升点击返回按钮 -> 杀进程。")
                    AnalysisUtils.analysis(AnalysisConstants.EVENT_UPGRADE, AnalysisConstants.KEY_UPDATE_ACTIVITY_STATUS, updateActivityStatus.toString())
                    com.blankj.utilcode.util.AppUtils.exitApp()
                }
            } else {
                updateActivityStatus.append("非强升点击返回按钮 -> 关闭页面。")
                AnalysisUtils.analysis(AnalysisConstants.EVENT_UPGRADE, AnalysisConstants.KEY_UPDATE_ACTIVITY_STATUS, updateActivityStatus.toString())
                finish()
            }
        }
    }

}